package org.lanqiao.tjut.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.tjut.bean.UsersBean;
import org.lanqiao.tjut.myUtils.MyUtils;
import org.lanqiao.tjut.service.ServiceFactory;

public class UserInfoModifyServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 准备用户进行修改的数据
		String strOperationType = request.getParameter("opt");
		int iOpt = MyUtils.obj2Int(strOperationType);
		// 结果跳转页面
		String strJumpPage = "";

		if (iOpt == 0) {
			// 修改
			// 修改跳转操作

			// 获取id主键值
			String strUid = request.getParameter("uid");
			UsersBean usersB = ServiceFactory.getUserInfoManagerServiceInstance().getUserInfoById(strUid);
			// 将数据缓存到session对象中
			request.setAttribute("usersB", usersB);
			/*// 修改显示性别选项
			if(usersB.getUsersex().equals("男")){
				request.setAttribute("userSexM", "checked");
				//清空nv的标志变量
				request.setAttribute("userSexF", "");
			}else{
				request.setAttribute("userSexF", "checked");
				request.setAttribute("userSexM", "");
			}*/
			
			// 跳转修改结果页
			strJumpPage = "logined/UserInfoManager/userInfoAdd.jsp";
		} else if (iOpt == 1) {
			// 删除
			int userId = MyUtils.obj2Int(request.getParameter("uid"));
			//数据库删除
			int i = ServiceFactory.getUserInfoManagerServiceInstance().deleteUserById(userId);
			//跳转页面
			strJumpPage = "UserInfoModifyServlet?opt=2";
		} else if (iOpt == 2) {
			// 判定当前的操作是查询还是修改跳转
			// 查询操作
			// 2、将保单数据填充到实体类
			UsersBean usersB = (UsersBean) MyUtils.getNewInstance(UsersBean.class);

			usersB.setUsername(request.getParameter("txtUserName"));
			usersB.setUserage(MyUtils.obj2Int(request.getParameter("txtUserAge")));
			usersB.setUserage2(MyUtils.obj2Int(request.getParameter("txtUserAge2")));
			usersB.setUseraddress(request.getParameter("txtUserAddress"));
			usersB.setUsersex(request.getParameter("selectSext"));
			usersB.setUsertel(request.getParameter("txtUserTel"));

			// 查询操作
			List<UsersBean> lstUsers = ServiceFactory.getUserInfoManagerServiceInstance().doQueryByParas(usersB);

			// 将需要显示的结果集放入session缓存中
			request.getSession().setAttribute("lstUsers", lstUsers);
			//查询结果页
			strJumpPage = "logined/UserInfoManager/userInfoAddDetail.jsp";
		}
		// 跳转到修改页面
		request.getRequestDispatcher(strJumpPage).forward(request, response);
	}

}
